package com.zijing2333.adaptivescheduling.SwitcherManager.server;




import android.util.Log;

import com.zijing2333.adaptivescheduling.ChannelManager;
import com.zijing2333.adaptivescheduling.SwitcherManager.TaskInfoManager;
import com.zijing2333.adaptivescheduling.SwitcherManager.flow.StringDataFlow;
import com.zijing2333.adaptivescheduling.criticalNodeJudgment.JudgeKeyNode;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 该类的一个的对象和某个客户端保持通信，这个类主要是接收客户端发来的数据 run方法中主要是接收数据   write开头的方法主要是发送数据
 */

public class ServerConnectClientThread implements Runnable{
    private static final String TAG = "fun_ServerConnectClientThread_time";
    private Socket mSocket; //主要用在服务端接收客户器端数据中
    private  ServerCallback mCallback;//主要用在服务端接收客户器端数据中

    private DataInputStream mDis;//主要用在服务端接收客户器端数据中

    private DataOutputStream mDos;//主要用在服务端发送数据给客户器端中

    private InputStream inputStream;
    private OutputStream outputStream;

    private final int MIGRATION_DATA = 1; //携带服务的状态数据
    private final int NOTICE = 2; //通知  对端接收后还需要对是否是关键节点进行判断


    private final int MIGRATION_IP = 3; //携带源ip地址
    private boolean mIsRunning = false;//主要标志客户端和服务器端是否保持连接的状态


    public ServerConnectClientThread(Socket socket, ServerCallback callback){
        this.mSocket=socket;
        this.mCallback=callback;
        try {
            inputStream = socket.getInputStream();
            mDis = new DataInputStream(inputStream);
            outputStream = socket.getOutputStream();
            mDos = new DataOutputStream(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public void run() {
        setRunning(true);
        while (isRunning()){
//            Log.d(TAG,  "socket服务端接收到消息-----------------");
            try{
                int messageType =mDis.readInt();
                String JsServiceId = mDis.readUTF();
                String clientIp = mDis.readUTF();
                if (messageType == 3){ //表示B设备给A设备发送B设备本机的IP地址，可以帮助A设备建立任务与ServerConnectClient线程的对应关系
                    mCallback.receiveClientMsg(JsServiceId,clientIp);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        close();
    }
    private void setRunning(boolean running) {
        mIsRunning = running;
    }
    private void stopRunning() {
        mIsRunning = false;
    }
    public boolean isRunning() {
        return mIsRunning;
    }

    public void writeString(String ServiceId,String type,String data) {
        try {
            if ("1".equals(type)){
                Log.d(TAG, "writeString:发送过去的数据"+data+",任务id："+ServiceId);
                mDos.writeInt(MIGRATION_DATA); //这个是区别发送通知和数据的变量
                mDos.writeUTF(ServiceId);
                mDos.writeUTF(data);
            }else if ("2".equals(type)){
                mDos.writeInt(NOTICE); //这个是区别发送通知和数据的变量
                mDos.writeUTF(ServiceId);

            }else if ("3".equals(type)){
                mDos.writeInt(MIGRATION_IP); //这个是区别发送通知和数据的变量
                mDos.writeUTF(ServiceId);
                mDos.writeUTF(data);
            }
            mDos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void close() {
        Log.d(TAG, "服务端socket线程关闭========================");
        stopRunning();
        try {
            if (mDos != null){
                mDos.close();
            }
            if (outputStream !=null){
                outputStream.close();
            }
            if (mDis != null){
                mDis.close();
            }
            if (inputStream!=null){
                inputStream.close();
            }
            if (mSocket != null) {
                mSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
